import React, {Component} from 'react';
import './index.less';
import {base} from 'nc-lightapp-front';
import resetFormInfo from "./functions/resetFormInfo";
import getPsnInfoByID from "../../container/functions/getPsnInfoByID";

const unitConf = {
    multiLang: {
        domainName: 'uapbd',
        currentLocale: 'zh-CN',
        moduleId: 'refer_uapbd',
    },
    key: 'pk_country',
    refType: 'grid',
    refName: 'refer-000444', /* 国际化处理： 国家地区EX*/
    placeholder: 'refer-000444', /* 国际化处理： 国家地区EX*/
    refCode: 'uapbd.refer.pubinfo.CountryExDefaultGridRef',
    queryGridUrl: '/nccloud/uapbd/pub/CountryExDefaultGridRef.do',
    isMultiSelectedEnabled: false,
    columnConfig: [{
        name: ['refer-000002', 'refer-000441', 'refer-000003', 'refer-000422', 'refer-000442', 'refer-000443', 'refer-000445'], /* 国际化处理： 编码,三位代码,名称,描述,时区,格式,欧盟国家*/
        code: ['refcode', 'codeth', 'refname', 'description', 'timezonename', 'formatname', 'iseucountry'],
        checked: {
            description: false,
            timezonename: false,
            formatname: false,
            iseucountry: false,
        }
    }]
};

class AddNew extends Component {
    constructor(props) {
        super(props);
        this.onBeforeFormEvent = this.onBeforeFormEvent.bind(this);
        this.onAfterFormEvent = this.onAfterFormEvent.bind(this);
    }

    onBeforeFormEvent(props, moduleId, key, value, index) {
        if (key === 'psncode') {
            let that = this;
            let meta = that.props.meta.getMeta();
            meta[moduleId].items.find(item => item.attrcode === key).queryCondition = function () {
                return {
                    "pk_org": (that.props.orgVal && that.props.orgVal.refpk),
                    "isIncludeKeyPsnGrp": "false",
                    "GridRefActionExt": "nccloud.web.hrhi.blacklist.sqlbuilder.BlackListPsnRefSqlBuilder"
                }
            };
            that.props.meta.setMeta(meta);
        } else if (key === 'permanentres') {
            let that = this;
            let meta = that.props.meta.getMeta();
            const permanentres = meta[moduleId].items.find(item => item.attrcode === 'permanentres');
            permanentres.isShowUnit = true;
            permanentres.unitProps = unitConf;
            that.props.meta.setMeta(meta);
        }
        return true;
    }

    onAfterFormEvent(props, moduleId, key, value, oldValue) {
        if (key === 'psncode') {
            resetFormInfo.call(this);
        } else if (key === 'id') {
            //输入身份证时自动获取性别和生日
            if (props.form.getFormItemsValue('blackcard', 'idtype').value === '1001Z01000000000AI36') {
                const info = getPsnInfoByID.call(this, value.value);
                if (info) {
                    props.form.setFormItemsValue('blackcard', info);
                }
            }
        }
    }

    render() {
        const {form} = this.props;

        return (
            <div className="psn-query-detail">
                {form.createForm('blackcard', {
                    onBeforeEvent: this.onBeforeFormEvent,
                    onAfterEvent: this.onAfterFormEvent
                })}
            </div>
        );
    }
}

export default AddNew;
